package com.tusuo.stds.ui.school;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.AreaList;
import com.tusuo.stds.rest.SchoolList;
import com.tusuo.stds.rest.UserInfo;

import java.util.List;

/**
 * @author J.Huang
 * @version 1.0
 *
 */
public class SchoolListAdapter extends BaseAdapter {

    private List<SchoolList.School> dataList;
    private LayoutInflater layoutInflater;
    private Context mContext;
    private UserInfo mUserInfo;
    private int mLevel;

    public SchoolListAdapter(Context context, List<SchoolList.School> dataList, UserInfo userInfo) {
        this.mContext = context;
        this.dataList = dataList;
        this.layoutInflater = LayoutInflater.from(context);
        mUserInfo = userInfo;
    }

    private final class Component {
        TextView tvName;
        TextView tvSelected;
        ImageView ivArrow;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public SchoolList.School getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Component component = null;
        if (convertView == null) {
            component = new Component();
            // 获得组件，实例化组件
            convertView = layoutInflater.inflate(R.layout.area_list_item, null);

            component.tvName = (TextView) convertView.findViewById(R.id.area_list_item_tv_name);
            component.tvSelected = (TextView) convertView.findViewById(R.id.area_list_item_tv_selected);
            component.ivArrow = (ImageView) convertView.findViewById(R.id.area_list_item_iv_arrow);

            convertView.setTag(component);
        } else {
            component = (Component) convertView.getTag();
        }

        SchoolList.School school = dataList.get(position);

        // 绑定数据
        component.ivArrow.setVisibility(View.INVISIBLE);
        component.tvName.setText(school.Name);

        if (mUserInfo.SchoolId == school.Id) {
            component.tvSelected.setVisibility(View.VISIBLE);
        } else {
            component.tvSelected.setVisibility(View.GONE);
        }

        return convertView;
    }
}
